class DBService {
    constructor() {
        // 初始化本地存储
        if (!localStorage.getItem('users')) {
            localStorage.setItem('users', JSON.stringify([]));
        }
    }

    async registerUser(phone, password) {
        return new Promise((resolve, reject) => {
            try {
                const users = JSON.parse(localStorage.getItem('users') || '[]');
                
                // 检查手机号是否已注册
                if (users.some(user => user.phone === phone)) {
                    reject({ success: false, error: '该手机号已注册' });
                    return;
                }

                // 添加新用户
                users.push({
                    id: Date.now(),
                    phone,
                    password,
                    created_at: new Date().toISOString()
                });

                localStorage.setItem('users', JSON.stringify(users));
                resolve({ success: true });
            } catch (error) {
                reject({ success: false, error: error.message });
            }
        });
    }

    async verifyUser(phone, password) {
        return new Promise((resolve, reject) => {
            try {
                const users = JSON.parse(localStorage.getItem('users') || '[]');
                const user = users.find(u => u.phone === phone && u.password === password);
                
                resolve({ 
                    success: !!user,
                    user: user ? { ...user, password: undefined } : null
                });
            } catch (error) {
                reject({ success: false, error: error.message });
            }
        });
    }
}

const dbService = new DBService();
export default dbService; 